var preferencesService = null;

// Deletes a preference
function deletePreference(preference)
{
    // If the preference is set
    if(preference)
    {
        // If a user preference is set
        if(isPreferenceSet(preference))
        {
            getPreferencesService().clearUserPref(preference);
        }
    }
}

// Deletes a preference branch
function deletePreferenceBranch(branch)
{
    // If the branch is set
    if(branch)
    {
        getPreferencesService().deleteBranch(branch);
    }
}

// Gets a boolean preference, returning false if the preference is not set
function getBooleanPreference(preference, userPreference)
{
    // If the preference is set
    if(preference)
    {
        // If not a user preference or a user preference is set
        if(!userPreference || isPreferenceSet(preference))
        {
            alert("getBooleanPreference");
            try
            {
                return getPreferencesService().getBoolPref(preference);
            }
            catch(exception)
            {
                // Do nothing
            }
        }
    }

    return false;
}

// Gets an integer preference, returning 0 if the preference is not set
function getIntegerPreference(preference, userPreference)
{
    // If the preference is set
    if(preference)
    {
        // If not a user preference or a user preference is set
        if(!userPreference || isPreferenceSet(preference))
        {
            try
            {
                return getPreferencesService().getIntPref(preference);
            }
            catch(exception)
            {
                // Do nothing
            }
        }
    }

    return 0;
}

// Gets the preferences service
function getPreferencesService()
{
    // If the preferences service is not set
    if(!preferencesService)
    {
        preferencesService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("app.ballbet.");
    }

    return preferencesService;
}

// Gets a string preference, returning null if the preference is not set
function getStringPreference(preference, userPreference)
{
    // If the preference is set
    if(preference)
    {
        // If not a user preference or a user preference is set
        if(!userPreference || isPreferenceSet(preference))
        {
            try
            {
                return getPreferencesService().getComplexValue(preference, Components.interfaces.nsISupportsString).data;
            }
            catch(e)
            {
                alert(e);
                // Do nothing
            }
        }
    }

    return null;
}

// Is a preference set
function isPreferenceSet(preference)
{
    // If the preference is set
    if(preference)
    {
        return getPreferencesService().prefHasUserValue(preference);
    }

    return false;
}

// Sets a boolean preference
function setBooleanPreference(preference, value)
{
    // If the preference is set
    if(preference)
    {
        getPreferencesService().setBoolPref(preference, value);
    }
}

// Sets a boolean preference if it is not already set
function setBooleanPreferenceIfNotSet(preference, value)
{
    // If the preference is not set
    if(!isPreferenceSet(preference))
    {
        getPreferencesService().setBoolPref(preference, value);
    }
}

// Sets an integer preference
function setIntegerPreference(preference, value)
{
    // If the preference is set
    if(preference)
    {
        getPreferencesService().setIntPref(preference, value);
    }
}

// Sets an integer preference if it is not already set
function setIntegerPreferenceIfNotSet(preference, value)
{
    // If the preference is not set
    if(!isPreferenceSet(preference))
    {
        setIntegerPreference(preference, value);
    }
}

// Sets a string preference
function setStringPreference(preference, value)
{
    // If the preference is set
    if(preference)
    {
        var supportsStringInterface = Components.interfaces.nsISupportsString;
        var string = Components.classes["@mozilla.org/supports-string;1"].createInstance(supportsStringInterface);

        string.data = value;

        getPreferencesService().setComplexValue(preference, supportsStringInterface, string);
    }
}

// Sets a string preference if it is not already set
function setStringPreferenceIfNotSet(preference, value)
{
    // If the preference is not set
    if(!isPreferenceSet(preference))
    {
        setStringPreference(preference, value);
    }
}